package com.kum.controller;

import com.kum.domain.constant.AjaxResult;
import com.kum.domain.dto.RegisterDto;
import com.kum.domain.entity.SysSign;
import com.kum.domain.entity.SysUser;
import com.kum.service.SignService;
import com.kum.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version V1.0
 * @Package com.kum.controller
 * @auhter 枯木Kum
 * @date 2021/3/9-7:46 PM
 */

@RestController
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SignService signService;


    @GetMapping("/list")
    public AjaxResult list() {
        //返回的列表去除已签到的用户
        List<SysUser> userList = sysUserService.list();
        List<SysSign> cacheSignList = signService.getCacheSignList();
        ArrayList<Integer> isSignList = new ArrayList<>();
        cacheSignList.forEach(s -> isSignList.add(s.getSysUser().getId()));
        return AjaxResult.success(
                userList.stream().filter(
                        (u) -> !isSignList.contains(u.getId()))
                        .collect(Collectors.toList())
        );
    }

    /**
     * 注册用户
     * todo 校验提交的信息
     * <p>注册用户,并存储用户的人脸数据</p>
     *
     * @param registerDto
     * @return
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterDto registerDto) {
        sysUserService.register(registerDto);
        return AjaxResult.success();
    }
}